﻿using Approval.Domain;
using AutoMapper;
using MediatR;
using PublicCommon;
using System.Collections.Generic;
using T_settlement.API.Application.Command.PaymentCommand;
using T_settlement.Domain;
using T_settlement.Infrastructure.Interfacec;

namespace T_settlement.API.Application.CommandHandlers.PaymentCommandHandlers
{
    public class AddPaymentCommandHandlers:IRequestHandler<AddPaymentCommand,int>
    {
        private readonly IPaymentRepository paymentRepository;
        private readonly IMapper mapper;
        private readonly Cross_services_Helper cross_Services_Helper;


        public AddPaymentCommandHandlers(IPaymentRepository paymentRepository, IMapper mapper, Cross_services_Helper cross_Services_Helper)
        {
            this.paymentRepository = paymentRepository;
            this.mapper = mapper;
            this.cross_Services_Helper = cross_Services_Helper;
        }

        public async Task<int> Handle(AddPaymentCommand request, CancellationToken cancellationToken)
        {

         
            var info = mapper.Map<Payment>(request);
            info.CreateBy = "王小宝";
            info.CreateDate = DateTime.Now;
            info.AuditState = request.AuditState = 1;//待审核        
            await paymentRepository.AddAsync(info);
 
            var DTO = new PaymentDTO();
            DTO.payId = info.PaymentId.ToString();
            DTO.approvalId = 4;
            DTO.approvalName = "小明";
            var message = await cross_Services_Helper.Post("http://10.31.60.9:8000/Approval/api/PayRelation/PayRelationAdd", DTO);

            if (message.LastIndexOf("状态码") > 0)
            {
                return await Task.FromResult(-1);
            }
            return await Task.FromResult(info.PaymentId);
            //var info = new Payment();
            //info.Title = request.Title;
            //info.Description = request.Description;
            //info.PaymentMoney = request.PaymentMoney;
            //info.SettlType = request.SettlType;
            //info.Tinning=request.Tinning;
            //info.Bank = request.Bank;
            //info.BankAccount = request.BankAccount;
            //info.CreateBy = "王小宝";
            //info.PaymentTime = request.PaymentTime;
            //info.Remark = request.Remark;
            //info.CreateDate = DateTime.Now;
            //info.AuditState = request.AuditState = 1;//待审核
            //info.InvoiceImg = request.InvoiceImg;
            //return await paymentRepository.AddAsync(info);
        }
    }
}
